package com.glacier.yuyuan.gen.webapp.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.gen.webapp.domain.GenFieldType;
import com.glacier.yuyuan.gen.webapp.service.GenFieldTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2022-11-10 15:12
 * 字段类型管理 控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("/genFieldType")
@Controller
public class GenFieldTypeController extends BaseController {

    private GenFieldTypeService genFieldTypeService;

    @Autowired
    public void setGenFieldTypeService(GenFieldTypeService genFieldTypeService) {
        this.genFieldTypeService = genFieldTypeService;
    }

    @GetMapping("")
    public String list() {
        return "view/genFieldType/genFieldTypeList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<GenFieldType>> page(Page<GenFieldType> page, GenFieldType genFieldType) {
        page = genFieldTypeService.findPage(page, genFieldType);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(GenFieldType genFieldType, Model model) {
        if (StrUtil.isNotBlank(genFieldType.getId())) {
            genFieldType = genFieldTypeService.findById(genFieldType.getId());
        }
        model.addAttribute("entity", genFieldType);
        return "view/genFieldType/genFieldTypeForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(GenFieldType genFieldType) {
        return Result.ok("操作成功！", genFieldTypeService.save(genFieldType));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        genFieldTypeService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
